package applet;

import java.awt.Graphics;

/** */
public abstract class Screen implements Runnable {
	/** */
	public static final int WIDTH = 640;
	/** */
	public static final int HEIGHT = 480;
	/** */
	public static final int FPS = 60;

	/**
	 * The {@linkplain GameApplet} which this screen is to be displayed in.
	 */
	private final Game game;
	/** */
	private final Fps limit;

	/**
	 * @param theGame
	 *            - the {@link GameApplet} which this screen is to be displayed
	 *            in.
	 */
	public Screen(final Game theGame) {
		game = theGame;
		limit = new Fps(FPS);
	}

	/** @return the number of frames performed in the last second. */
	public final int getCurrentFps() {
		return limit.getCurrentFps();
	}

	/** @return the {@linkplain Game} which this screen is attached to. */
	public final Game getGame() {
		return game;
	}

	/**
	 * Called by the {@linkplain GameApplet} when the screen is entered.
	 */
	public final void run() {
		begin();
		game.draw(this);
		limit.begin();
		while (game.getScreen() == this) {
			step();
			game.draw(this);
			limit.step();
		}
		end();
	}

	/**
	 * Called once when the screen begins. Use this for any resource allocation.
	 */
	public void begin() {
		// Do nothing
	}

	/**
	 * Called once when the screen ends. Use this for any resource cleaning.
	 */
	public void end() {
		// Do nothing
	}

	/**
	 * Called repetitively until the screen is ended.
	 */
	public void step() {
		// Do nothing
	}

	/**
	 * Called by the {@linkplain Game} shortly after a call to {@code redraw()}
	 * or immediately after a call to {@code redrawNow()}.
	 * 
	 * @param g
	 *            - a {@linkplain Graphics} object to draw the screen to.
	 */
	public void draw(final Graphics g) {
		// Do nothing
	}
}
